package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.PrBankinfoDTO;
import com.financing.wap.service.IPrBankinfoService;

@Controller("prBankinfoController")
@RequestMapping("/api/prBankinfo")
public class PrBankinfoController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IPrBankinfoService prBankinfoService;
	
	@RequestMapping("/listPrBankinfo")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, PrBankinfoDTO prBankinfo,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query prBankinfo list begin .......");
			
			prBankinfo.setBegin(pager.getStartIndex());
			prBankinfo.setPageSize(pager.getPageSize());
			
			int count = prBankinfoService.queryListCount(prBankinfo);
			
			pager.setTotalCount(count);
			
			List<PrBankinfoDTO> prBankinfoList = null;
			
			if(count > 0){
				prBankinfoList = prBankinfoService.queryList(prBankinfo);
			}else{
				prBankinfoList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", prBankinfoList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query prBankinfo list end .......");
		}catch(Exception e){
			log.error("query prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addPrBankinfo")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, PrBankinfoDTO prBankinfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add prBankinfo  begin .......");
			
			DTOdateSetUtil.setCreateInfo(prBankinfo);
			DTOdateSetUtil.setUpdateInfo(prBankinfo);
			
			int id = prBankinfoService.insert(prBankinfo);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add prBankinfo list end .......");
		}catch(Exception e){
			log.error("add prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailPrBankinfo")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, PrBankinfoDTO prBankinfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail prBankinfo  begin .......");
			
			Integer id = prBankinfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			PrBankinfoDTO dto = prBankinfoService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail prBankinfo  end .......");
		}catch(Exception e){
			log.error("query detail prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deletePrBankinfo")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, PrBankinfoDTO prBankinfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete prBankinfo  begin .......");
			
			Integer id = prBankinfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			prBankinfoService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete prBankinfo  end .......");
		}catch(Exception e){
			log.error("delete prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updatePrBankinfo")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, PrBankinfoDTO prBankinfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update prBankinfo  begin .......");
			
			Integer id = prBankinfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prBankinfo);
			
			prBankinfoService.update(prBankinfo);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update prBankinfo list end .......");
		}catch(Exception e){
			log.error("update prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForcePrBankinfo")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, PrBankinfoDTO prBankinfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce prBankinfo  begin .......");
			
			Integer id = prBankinfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prBankinfo);
			
			prBankinfoService.updateForce(prBankinfo);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce prBankinfo list end .......");
		}catch(Exception e){
			log.error("updateForce prBankinfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
